package com.itheima.admin.service.impl;

import com.itheima.admin.dto.ModuleDto;
import com.itheima.admin.mapper.ModuleMapper;
import com.itheima.admin.mapper.RoleMapper;
import com.itheima.admin.pojo.Module;
import com.itheima.admin.service.IModuleService;
import com.itheima.admin.vo.ModuleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ModuleServiceImpl implements IModuleService {

    @Autowired
    private ModuleMapper moduleMapper;

    private List<ModuleVo> getModuleVos(List<Module> moduleIPage) {
        if(moduleIPage==null){
            return new ArrayList<>();
        }
        return moduleIPage.stream().map(
                module -> {
                    ModuleVo moduleVo = new ModuleVo();
                    BeanUtils.copyProperties(module, moduleVo);
                    return moduleVo;
                }
        ).collect(Collectors.toList());
    }

    @Override
    public List<ModuleVo> queryAll() {
        List<Module> modules = moduleMapper.selectList(null);
        return getModuleVos(modules);
    }

    @Override
    public boolean addModule(ModuleDto moduleDto) {
        int insert = moduleMapper.insert(moduleDto.toModule());
        return insert>0;
    }

    @Autowired
    private RoleMapper roleMapper;

    public boolean deleteRoleModule(String id) {
        //角色和模块
//        roleMapper.deleteRoleModuleByModuleId(id);
        return moduleMapper.deleteModule(id)>0;
    }
}
